
//这个函数在整个wps加载项中是第一个执行的
function OnAddinLoad(ribbonUI) {
    if (typeof (wps.ribbonUI) != "object") {
        wps.ribbonUI = ribbonUI
    }

    if (typeof (wps.Enum) != "object") { // 如果没有内置枚举值
        wps.Enum = WPS_Enum
    }

    wps.PluginStorage.setItem("EnableFlag", false) //往PluginStorage中设置一个标记，用于控制两个按钮的置灰
    wps.PluginStorage.setItem("ApiEventFlag", false) //往PluginStorage中设置一个标记，用于控制ApiEvent的按钮label
    wps.PluginStorage.setItem("tocShowHiddenSheet", false) // 目录列表是否显示被隐藏工作表 TODO:可以从配置文件读取
    wps.PluginStorage.setItem("listAllTocLi",false)// 是否展开所有的目录；
    wps.PluginStorage.setItem("toc_onlyActiveWorkbook",false) // 仅显示激活的工作簿。

    // 尝试读取用户配置文件
    let configFile = wps.Env.GetHomePath() + "/hcet.json";
    let f = wps.FileSystem.Exists(configFile)
    if (f) {
        let confContent = wps.FileSystem.ReadFile(configFile)
        let confs = JSON.parse(confContent)
        let k = Object.keys(confs)
        for (let i = 0; i < k.length; i++) {
            let item_key = k[i];
            let item_value = confs[item_key];
            // 注册用户配置到 wps.PluginStorage
            wps.PluginStorage.setItem(item_key, item_value)
        }
    }
    return true
}

var WebNotifycount = 0;
function OnAction(control) {
    const eleId = control.Id
    switch (eleId) {
        case "summary":
            taskpaneToggle('summary',"/toc/toc.html")
            break
        case "shang":
            let url = GetUrlPath()+"/ui/shang.html";
            wps.ShowDialog(url,"打赏作者",400 * window.devicePixelRatio,400 * window.devicePixelRatio,undefined)
            break
        case "book":
            wps.OAAssist.ShellExecute("explorer","https://www.kancloud.cn/pwedu/wps-js-macros")
            break
        case "insiderSummary":
            let urlp = GetUrlPath()+"/ui/addInsiderSummary.html";
            wps.ShowDialog(urlp,"插入表内目录",400 * window.devicePixelRatio,400 * window.devicePixelRatio,false)
            break
        case "sources":
            wps.OAAssist.ShellExecute("explorer","https://gitee.com/class877/hcet")
            break
        case "jcc":
            taskpaneToggle("jcc","/papaparse/jcc.html")
            break
        case "snippets":
            taskpaneToggle("snippets", "https://www.kancloud.cn/book/pwedu/macro-snippets")
            break
        case "lodash":
            taskpaneToggle("lodash","/ui/lodash.html")
            break
        case "fileSystem":
            let u = GetUrlPath()+"/fileSystem/index.html";
            wps.ShowDialog(u,"文件读写",800 * window.devicePixelRatio,800 * window.devicePixelRatio,false)
            break
        case "jsAce":
            wps.ShowDialog("https://www.pwedu.top/wpsaddon/singlepage/aceForWps-et.html","jsEditor",800 * window.devicePixelRatio,800 * window.devicePixelRatio,false)
            break
        default:
            break
    }
    return true
}
/**
 * 设置菜单控件图标(基于控件ID绑定)
 * @param {*} control 
 * @returns 
 */
function GetImage(control) {
    const eleId = control.Id
    switch (eleId) {
        case "summary":
            return "images/summary.svg"
        case "sources":
            return "images/git.svg"
        case "book":
            return "images/book.svg"
        case "setting":
            return "images/setting.svg"
        case "shang":
            return "images/shang.svg"
        case "insiderSummary":
            return "images/toc.svg"
        case "fileSystem":
            return "images/io.svg"
        case "jsAce":
            return "images/editor128.svg"
        case "jcc":
            return "images/jcc.svg"
        case "lodash":
            return "images/json.svg"
        case "snippets":
            return "images/snippet.svg"
        default:
            ;
    }
    return "images/newFromTemp.svg"
}